<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <ui:composition template="../templates/master.xhtml">
            <ui:define name="content">

                <p:dataTable rowIndexVar="d" value="#{complaintsController.complaints}" paginator="true" rows="10"
                             paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" 
                             var="c" rowsPerPageTemplate="5,10,15" id="carTable" lazy="true">

                    <p:column styleClass="complaint-status-#{c.statusName} row-item-#{c.id}"  headerText="ID">
                        <h:outputText value="[#{c.id}]" />
                        <br/>
                        <h:outputText value="#{c.lastUpdated}">
                            <f:convertDateTime pattern="dd-MMM-yyyy"/>
                        </h:outputText>
                    </p:column>
                    <p:column filterBy="#{c.title}" sortBy="#{c.title}" headerText="Title">
                        <h:outputText value="#{c.title}"/>
                    </p:column>
                    <p:column sortBy="#{c.categoryName}" headerText="Category" filterBy="#{c.categoryName}">
                        <h:outputText value="#{c.categoryID.name}"/>
                    </p:column>
                    <p:column headerText="">
                        <p:commandButton action="#{complaintsController.prepareView()}" icon="ui-icon-search">
                            <f:setPropertyActionListener target="#{complaintsController.selected}" value="#{c}"/>
                        </p:commandButton>
                        <p:commandButton ajax="false" action="#{complaintsController.prepareEdit()}" icon="ui-icon-pencil">
                            <f:setPropertyActionListener target="#{complaintsController.selected}" value="#{c}"/>
                        </p:commandButton>
                        <p:commandButton oncomplete="onDeleteRowComplete(xhr, status,args)" action="#{complaintsController.destroy()}" icon="ui-icon-close" immediate="true">
                            <f:setPropertyActionListener target="#{complaintsController.selected}" value="#{c}"/>
                        </p:commandButton>

                    </p:column>

                </p:dataTable>
                <p:commandButton action="#{complaintsController.prepareCreate()}" value="Create"/>
            </ui:define>
             <ui:define name="footer">
                <ui:include src="../templates/footer.xhtml"/>
                <script type="text/javascript">
                    function onDeleteRowComplete(xhr, status,args){
                        var item = args.item;
                        $("td.row-item-"+item).closest("tr").fadeOut("fast").remove();
                        console.log(item);
                    }
                </script>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

